<template>
  <t-dialog header="评审判定" :visible.sync="visible" :onClose="closeDialog" :closeOnOverlayClick="false"
    @confirm="onSubmit" :confirmBtn="{
    content: loading ? '提交中...' : '确认',
    theme: 'primary',
    loading: loading,
  }" width="60%" top="5%">
    <div style="height: 65vh">
      <t-form ref="form" :colon="true" resetType="initial" :data="formData" labelWidth="150px" :rules="rules">

        <t-form-item label="项目名称">
          <t-link v-if="formData.version === '2024'" @click="dialog.header = projectName; dialog.open_item_detail = true"
            hover="color" theme="primary">{{ projectName }}</t-link>
          <t-link v-else @click="dialog.header = projectName; dialog.open_project_detail = true" hover="color"
            theme="primary">{{ projectName }}</t-link>
        </t-form-item>
        <t-form-item label="评审类型" name="reviewType">
          <!--          <t-select style="width: 390px" disabled placeholder="评审业务类型" clearable filterable-->
          <!--                    :options="dict.type.review_type"-->
          <!--                    v-model="formData.reviewType">-->
          <!--          </t-select>-->
          {{ formData.reviewType | filterByDict(dict.type.review_type) }}
        </t-form-item>
        <t-form-item v-if="reviewType === '1'" label="评审平均分">
          {{ formData.sumScore }}
        </t-form-item>
        <t-form-item label="是否合格" name="projectStatus" v-if="reviewType === '3'">
          <t-radio-group v-model="formData.projectStatus">
            <t-radio value="3" v-if="reviewType === '3'"> 优秀</t-radio>
            <t-radio value="1"> 合格</t-radio>
            <t-radio value="2" v-if="reviewType != '1' && reviewType != '4'">需要复议</t-radio>
            <t-radio value="0"> 不合格</t-radio>
          </t-radio-group>
        </t-form-item>
        <t-form-item label="是否通过" name="projectStatus" v-else>
          <t-radio-group v-model="formData.projectStatus">
            <t-radio value="3" v-if="reviewType === '3'"> 优秀</t-radio>
            <t-radio value="1"> 通过</t-radio>
            <t-radio value="2" v-if="reviewType != '1' && reviewType != '4'">需要复议</t-radio>
            <t-radio value="0"> 不通过</t-radio>
          </t-radio-group>
        </t-form-item>
        <!-- 通过 第一次立项评审 一般科技项目 -->
        <t-form-item
          v-if="formData.projectStatus === '1' && formData.reviewType === '4' && ![1662099020236, 1660720633654].includes(formData.projectCategory)"
          label="是否需要二次评审" name="secondReviewType">
          <t-radio-group v-model="formData.secondReviewType">
            <t-radio value="1"> 是</t-radio>
            <t-radio value="0"> 否</t-radio>
          </t-radio-group>
        </t-form-item>
        <template v-if="formData.projectStatus === '1' && ['1', '4'].includes(reviewType)">
          <t-form-item v-if="isShow" label="项目编号" name="projectNum">
            <t-input clearable style="width: 390px" placeholder="请输入项目编号" v-model="formData.projectNum"></t-input>
          </t-form-item>
          <t-form-item v-if="site_tag === 'jtt'" label="计划类别" name="projectCategory">
            <t-cascader @change="onProjectCategoryChange" v-if="reviewType === '4'" clearable filterable
              style="width: 390px" v-model="formData.projectCategory" :options="categoryOptions" />
            <t-cascader @change="onProjectCategoryChange" v-else clearable filterable style="width: 390px"
              :keys="{ value: 'id', label: 'name' }" v-model="formData.projectCategory" :options="projectCategoryData" />
          </t-form-item>
          <t-form-item v-if="formData.projectCategory == '1662099020236'" label="专家评审附件" name="file">
            <file-upload v-model="formData.project.sysAttMainVo.sysAttMainIds" accept="application/pdf,image/*"
              moduleKey="kjxmZjpsFile" :fileList="fileLists" :multiple="true" tips="">
              <t-button variant="outline">
                <t-icon name="cloud-upload" slot="icon" />
                上传
              </t-button>
            </file-upload>
          </t-form-item>
          <t-form-item v-if="isShow" label="学科分类" name="subjectClassification">
            <t-select clearable style="width: 390px" v-model="formData.subjectClassification"
              :options="dict.type.subject_classification">
            </t-select>
          </t-form-item>
          <template v-if="reviewType !== '4'">
            <t-form-item v-if="![1662099020236, 1660720633654].includes(formData.projectCategory)" label="项目类型"
              name="projectType">
              <t-select clearable style="width: 390px" v-model="formData.projectType" :options="dict.type.project_type">
              </t-select>
            </t-form-item>
          </template>

        </template>
        <template v-else-if="formData.projectStatus === '1' && reviewType === '5'">
          <t-form-item v-if="isShow" label="项目编号" name="projectNum">
            <t-input clearable style="width: 390px" placeholder="请输入项目编号" v-model="formData.projectNum"></t-input>
          </t-form-item>
          <t-form-item v-if="site_tag === 'jtt'" label="计划类别" name="projectCategory">
            <t-cascader @change="onProjectCategoryChange" clearable filterable style="width: 390px"
              v-model="formData.projectCategory" :options="categoryOptions" />
          </t-form-item>
          <t-form-item v-if="isShow" label="学科分类" name="subjectClassification">
            <t-select clearable style="width: 390px" v-model="formData.subjectClassification"
              :options="dict.type.subject_classification">
            </t-select>
          </t-form-item>
          <!--          <t-form-item v-if="formData.version !== '2024'" label="项目类型" name="projectType">-->
          <!--            <t-select clearable style="width: 390px" v-model="formData.projectType"-->
          <!--                      :options="dict.type.project_type">-->

          <!--            </t-select>-->
          <!--          </t-form-item>-->
        </template>

        <!--        <t-form-item label="建议经费总概算" name="adviceFunding">-->
        <!--          <t-input-adornment append="万元">-->
        <!--            <t-input-number :decimal-places="2"-->
        <!--                            @keyup="()=>{if(formData0.adviceFunding){formData0.adviceFunding=Number((formData0.adviceFunding || 0).toString().match(/^\d+(?:\.\d{0,2})?/))}}"-->
        <!--                            :allowInputOverLimit="false" clearable style="width: calc(390px - 45px)"-->
        <!--                            v-model="formData0.adviceFunding"-->
        <!--                            theme="normal"-->
        <!--                            :min="0"></t-input-number>-->
        <!--          </t-input-adornment>-->
        <!--        </t-form-item>-->
        <!--        <t-form-item :label="site_tag==='jtt'?'申请厅补助经费':'申请集团补助经费'" name="subsidyFunding">-->
        <!--          <t-input-adornment append="万元">-->
        <!--            <t-input-number :decimal-places="2"-->
        <!--                            @keyup="()=>{if(formData0.subsidyFunding){formData0.subsidyFunding=Number((formData0.subsidyFunding || 0).toString().match(/^\d+(?:\.\d{0,2})?/))}}"-->
        <!--                            :allowInputOverLimit="false" clearable style="width: calc(390px - 45px)"-->
        <!--                            v-model="formData0.subsidyFunding"-->
        <!--                            theme="normal"-->
        <!--                            :min="0"></t-input-number>-->
        <!--          </t-input-adornment>-->
        <!--        </t-form-item>-->
        <t-form-item label="备注" name="content">
          <t-textarea clearable :autosize="{ minRows: 5 }" v-model="formData.content" placeholder="请输入内容" />
        </t-form-item>
      </t-form>
      <project-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_project_detail"
        v-if="dialog.open_project_detail"></project-detail-dialog>
      <item-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_item_detail"
        v-if="dialog.open_item_detail"></item-detail-dialog>
    </div>
  </t-dialog>
</template>

<script>
import ProjectDetailDialog from "../../pm/components/project-detail-dialog.vue";
import ItemDetailDialog from "../../pm/components/item-detail-dialog.vue";
import FileUpload from "@/components/FileUpload/index.vue"
import { getFiles } from "@/api/common";

export default {
  name: "project-approval",
  dicts: ['sys_normal_disable', 'subject_classification', 'review_type', 'project_type'],
  components: {
    ProjectDetailDialog,
    ItemDetailDialog,
    FileUpload
  },
  props: ["objId", "reviewType", "projectName", "projectId"],
  data () {
    return {
      isShow: true,
      site_tag: import.meta.env.VITE_SITE_TAG,
      dialog: {
        header: '',
        open_project_detail: false,
        objId: null,
        open_item_detail: false,
      },
      fileLists: [],
      visible: true,
      loading: false,
      projectStatusOptions: [],
      projectCategoryData: [],
      categoryOptions: [],
      formData: {
        id: this.objId,
        projectStatus: '1',
        secondReviewType: '1',
        reviewType: this.reviewType,
        content: '',
        businessId: this.projectId,
        projectNum: '',
        projectCategory: '',
        subjectClassification: '',
        projectType: '',
        version: '',
        project: {
          sysAttMainVo: {
            sysAttMainIds: []
          }
        }
      },
      rules: {
        projectCategory: [
          { required: true, message: "计划类别不能为空" }
        ],
        subjectClassification: [
          { required: true, message: "学科分类不能为空" }
        ],
        projectType: [
          { required: true, message: "项目类型不能为空" }
        ],
        projectNum: [
          { required: true, message: "项目编号不能为空" }
        ],
        content: [
          { required: true, message: "评审意见不能为空" }
        ],
        score: [
          { required: true, validator: (val) => val >= 0, message: "大于等于0", type: "error" }
        ],
        projectStatus: [
          { required: true, message: "状态不能为空" }
        ]
      }
    }
  },
  async created () {
    await this.getPlanCategoryTreeSelect();
    this.getReviewResult();
    this.show();
    getFiles(this.objId).then((res) => {
      console.log(res)
      this.fileLists = []
      const ids = [];
      if (res.data && res.data.length) {
        res.data.forEach((item) => {
          ids.push(item.id);
          this.fileLists.push({ name: item.fileName, url: item.fullFilePath, id: item.id, filePath: item.filePath })
        })
        this.formData.sysAttMainVo.sysAttMainIds = ids;
      }
    });
  },
  methods: {
    show () {
      if (this.formData.reviewType === '4') {
        this.isShow = true;
        return
      }
      if (this.formData.reviewType === '5') {
        this.isShow = false;
        return
      }
      if (![1662099020236, 1660720633654].includes(this.formData.projectCategory)) {
        this.isShow = true;
        return
      }
      this.isShow = false;

    },
    onProjectCategoryChange (val) {
      this.$refs.form.reset();
      this.formData.projectCategory = val;
    },
    getPlanCategoryTreeSelect () {
      return this.$api.kjxm.project.planCategoryList().then(
        (res) => {
          this.projectCategoryData = this.handleTree(res.data, "id");
          let projectCategoryData = res.data.filter((val) => {
            if (!val.parentId) { return true }
          })
          this.categoryOptions = projectCategoryData.map((val) => ({ label: val.name, value: val.id }))
        }).catch((e) => {
          this.$message.error(e.toString());
        });
    },
    getReviewResult () {
      // 获取评审信息
      this.$api.kjxm.review.projectReviewTeamGet(this.objId).then(response => {
        if (response.data) {
          Object.assign(this.formData, response.data)
          this.formData.projectNum = response.data?.project?.projectNum;
          this.formData.subjectClassification = response.data?.project?.subjectClassification;
          this.formData.projectType = response.data?.project?.projectType;
          this.formData.projectCategory = response.data?.project?.projectCategory;
          this.formData.projectStatus = response.data.projectStatus || '1';
          this.formData.secondReviewType = response.data?.project?.secondReviewType || '0'
          this.formData.version = response.data?.project?.version;
          this.dialog.objId = response.data?.project?.id;
        }
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog () {
      this.$emit("update:visible", false);
    },
    onSubmit () {
      if (this.formData.projectStatus !== '1') {
        this.formData.secondReviewType = null;
      }
      console.log(this.formData)
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          // this.formData.id = this.formData.project.id
          this.$api.kjxm.review.teamProjectReviewUpdate(this.formData).then(reponse => {
            this.$message.success("提交成功");
            this.$emit("reload");
            this.$emit("update:visible", false);
            this.loading = false;
          }).catch((e) => {
            this.loading = false;
            this.$message.error(e.toString());
          });
        }
      })
    }
  }
}
</script>

<style scoped></style>
